#include "page.h"
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include "myFont.h"
#include "lv_compment.h"
#include "esp_log.h"
#include "esp_system.h"
#include "http.h"
#include "page2.h"
#include "lv_port_indev.h"
#include "deshow.h"
#include "weather.h"
#define ANIEND  while(lv_anim_count_running())lv_task_handler();//等待动画完成
LV_FONT_DECLARE(myFont);
extern lv_obj_t* scr_body;
extern struct DATA_WEATHER weather_data[3];
extern const struct LV_WEATHER lv_weather[40];
lv_obj_t* cont_l;
lv_obj_t * imgrain;
static lv_style_t style1;
lv_obj_t * label_data;
lv_obj_t * btn_l;
lv_obj_t * btn_r;
lv_obj_t * label_weather;
static void update_weather(uint8_t i)
{
     //天气图片
    char file[300];
    // sprintf(file,"/spiffs/%s",weather[weather_data[i].code_day].filename);//获取今天天气
	// PNG_SHOW(file, 48, 48,img_w,48);
    // img_rain.data=(const uint8_t *)img_w;
    lv_img_dsc_t *p=(lv_img_dsc_t *)lv_weather[weather_data[i].code_day].img_dsc;
    lv_img_set_src(imgrain,p);

    switch(i)
    {
    case 0:
    lv_label_set_text(label_data, "#FFA500 今天#");
        break;
    case 1:
    lv_label_set_text(label_data, "#FFA500 明天#");
        break;
    case 2:
    lv_label_set_text(label_data, "#FFA500 后天#");
        break;
    }
    sprintf(file,"#25c9e6 %s #\n #25c9e6 温度最高%d℃ 最低%d℃ #\n #25c9e6 %s风  %.1fkm/h #\n #25c9e6 湿度  %d %%#\n #25c9e6 降雨量  %.1f #\n",
                weather_data[i].text_day,
                weather_data[i].high,weather_data[i].low,
                weather_data[i].wind_direction,weather_data[i].wind_speed,
                weather_data[i].humidity,
                weather_data[i].rainfall
                );

    lv_label_set_text(label_weather,(const char *)file);
    //lv_obj_set_pos(label_weather,5,5);
   // lv_obj_set_style_local_value_line_space(label_weather,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,0);

}
static void event_handler(lv_obj_t * obj, lv_event_t event)
{
    static int8_t i=0;
    if(event == LV_EVENT_CLICKED) {
        if(obj==btn_l)
        {
            i--;printf("l_Clicked\n");
        }
        if(obj==btn_r)
        {
        i++;printf("r_Clicked\n");
        }

    }
    if(i<0)
        i=2;
    if(i>2)
        i=0;
    update_weather(i);
}
void page2_l_init()
{
    cont_l = lv_cont_create(scr_body, NULL);
    lv_cont_set_fit2(cont_l,LV_FIT_NONE,LV_FIT_NONE);
    lv_obj_set_size(cont_l,80,100);
    lv_obj_set_pos(cont_l,10,10);
    lv_obj_add_style(cont_l, LV_BTN_PART_MAIN, &style1);      /*Default button style*/
    //lv_cont_set_layout(cont_l, LV_LAYOUT_CENTER);

    imgrain = lv_img_create(cont_l,NULL);
    lv_obj_set_pos(imgrain,15,10);

    label_data=lv_label_create(cont_l,NULL);
    lv_label_set_align(label_data, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_style_local_text_font(label_data,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_label_set_recolor(label_data, true);
    lv_obj_set_pos(label_data,23,65);




    static lv_anim_path_t path_overshoot;
    lv_anim_path_init(&path_overshoot);
    lv_anim_path_set_cb(&path_overshoot, lv_anim_path_overshoot);

    static lv_anim_path_t path_ease_in_out;
    lv_anim_path_init(&path_ease_in_out);
    lv_anim_path_set_cb(&path_ease_in_out, lv_anim_path_ease_in_out);

    /*Gum-like button*/
    static lv_style_t style_gum;
    lv_style_init(&style_gum);
    lv_style_set_transform_width(&style_gum, LV_STATE_PRESSED, 3);
    lv_style_set_transform_height(&style_gum, LV_STATE_PRESSED, -3);
    lv_style_set_value_letter_space(&style_gum, LV_STATE_PRESSED, 5);
    lv_style_set_transition_path(&style_gum, LV_STATE_DEFAULT, &path_overshoot);
    lv_style_set_transition_path(&style_gum, LV_STATE_PRESSED, &path_ease_in_out);
    lv_style_set_transition_time(&style_gum, LV_STATE_DEFAULT, 250);
    lv_style_set_transition_delay(&style_gum, LV_STATE_DEFAULT, 100);
    lv_style_set_transition_prop_1(&style_gum, LV_STATE_DEFAULT, LV_STYLE_TRANSFORM_WIDTH);
    lv_style_set_transition_prop_2(&style_gum, LV_STATE_DEFAULT, LV_STYLE_TRANSFORM_HEIGHT);
    lv_style_set_bg_color(&style_gum, LV_STATE_DEFAULT, LV_COLOR_BLACK);//设置屏幕背景
    lv_style_set_border_color(&style_gum, LV_STATE_DEFAULT, LV_COLOR_BLACK);

    btn_l = lv_btn_create(cont_l, NULL);
    lv_obj_set_pos(btn_l,5,65);
    lv_obj_set_size(btn_l,15,15);
    lv_obj_add_style(btn_l, LV_BTN_PART_MAIN, &style_gum);

    /*Instead of creating a label add a values string*/
    lv_obj_set_style_local_value_color(btn_l, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
    lv_obj_set_style_local_value_str(btn_l, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_LEFT);



    btn_r = lv_btn_create(cont_l, NULL);
    lv_obj_set_pos(btn_r,60,65);
    lv_obj_set_size(btn_r,15,15);
    lv_obj_add_style(btn_r, LV_BTN_PART_MAIN, &style_gum);

    /*Instead of creating a label add a values string*/
    lv_obj_set_style_local_value_color(btn_r, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
    lv_obj_set_style_local_value_str(btn_r, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_RIGHT);

    lv_obj_set_event_cb(btn_l, event_handler);
    lv_obj_set_event_cb(btn_r, event_handler);
}
void page2_r_init()
{

    label_weather=lv_label_create(scr_body,NULL);
    lv_label_set_align(label_weather, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_style_local_text_font(label_weather,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_label_set_recolor(label_weather, true);
    lv_obj_set_pos(label_weather,85,20);

    update_weather(0);

}
compment_t cl,cr;
void page2_init()
{
    cl.start=-80;
    cl.end=5;
    cl.obj=cont_l;
    cl.start_t=600;
    cl.end_t=600;
    cl.cb=(void *)lv_anim_path_ease_out;
    cl.funcb=(void *)lv_obj_set_x;
    cl.next=NULL;

    cr.start=240;
    cr.end=90;
    cr.obj=label_weather;
    cr.start_t=600;
    cr.end_t=600;
    cr.cb=(void *)lv_anim_path_ease_out;
    cr.funcb=(void *)lv_obj_set_x;
    cr.next=NULL;

    lv_add_compment(&cl,&cr);
    obj_move(&cl,1);
    ANIEND

}
lv_group_t * group_button;
void group_init()
{
    group_button=lv_group_create();
    lv_group_add_obj(group_button, btn_l);//加入解码组
    lv_group_add_obj(group_button, btn_r);//加入解码组   
    lv_button_set_group(group_button);
}
void page2_load()
{
    // img_w=calloc(2 *48*48,sizeof(uint8_t));
    lv_style_set_bg_color(&style1, LV_STATE_DEFAULT, LV_COLOR_BLACK);//设置屏幕背景
  //  lv_style_set_border_color(&style1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    page2_l_init();
    page2_r_init();
    page2_init();
    group_init();

}
void page2_end()
{
    //ESP_LOGI("SYSTEM","正在退出界面2");
    obj_move(&cl,0);
    //ESP_LOGI("SYSTEM","移动");    
    ANIEND
    // free(img_w);
    //ESP_LOGI("SYSTEM","释放内存");
    if(cont_l!=NULL)
    lv_obj_clean(cont_l);//删除scr_body
    if(scr_body!=NULL)   
    lv_obj_clean(scr_body);//删除scr_body
    //ESP_LOGI("SYSTEM","esp_get_free_heap_size : %d  ", esp_get_free_heap_size());
}

void page2_start()
{
    page2_load();
    ESP_LOGI("SYSTEM","esp_get_free_heap_size : %d  ", esp_get_free_heap_size());
}
